<template>
  <button
    class="w-switch"
    :class="checked ? 'w-switch-checked' : ''"
    @click="switchHandel"
    :style="{
      'background-color': checked ? '#338eff' : '#00000040',
      transform:
        size === 'small'
          ? 'scale(.7, .7)'
          : size === 'large'
          ? 'scale(1.3, 1.3)'
          : 'scale(1, 1)',
    }"
  >
    <div class="w-switch-handel"></div>
    <span
      class="w-switch-text"
      :class="checked ? 'w-switch-text-checked' : 'w-switch-text-unchecked'"
      v-if="checkedText && uncheckedText"
      >{{ checked ? checkedText : uncheckedText }}</span
    >
  </button>
</template>

<script setup lang="ts">
import { ref } from 'vue'

interface Prop {
  checkedText?: string;
  uncheckedText?: string;
  size?: string;
}
withDefaults(defineProps<Prop>(), {
  checkedText: '',
  uncheckedText: '',
  size: 'normal'
})

const emits = defineEmits(['switch'])

const checked = ref<boolean>(false)

const switchHandel = () => {
  checked.value = !checked.value
  emits('switch', checked.value)
}
</script>

<style scoped lang="less">
@default-color: color("#338eff");
.w-switch {
  position: relative;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  min-width: 44px;
  height: 22px;
  line-height: 22px;
  border-radius: 100px;
  font-size: 14px;
  vertical-align: middle;
  border: 0;
  cursor: pointer;
  box-shadow: 0 0 0 2px #00000020;
  overflow: hidden;

  &:hover {
    box-shadow: none;
  }
  &::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: radial-gradient(circle, #fff 10%, transparent 10.1%);
    transform: scale(10);
    opacity: 0;
    transition: all 0.6s;
  }
  &:active::after {
    transform: scale(0);
    opacity: 0.7;
    transition: 0s;
  }
}

.w-switch-handel {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background-color: #fff;
  transition: all 0.2s ease-in-out;
}

.w-switch-checked .w-switch-handel {
  left: calc(100% - 20px);
}
.w-switch-checked {
  box-shadow: 0 0 0 2px fade(@default-color, 70%);
}

.w-switch-text {
  color: #fff;
  font-size: 12px;
  position: absolute;
  font-weight: 600;
}
.w-switch-text-checked {
  &:extend(.w-switch-text);
  left: 7px;
}
.w-switch-text-unchecked {
  &:extend(.w-switch-text);
  right: 7px;
}
</style>
